package cn.view;

import javax.swing.*;
import java.awt.*;
import cn.entity.Employee;
import cn.comon.UserSession;

public class UserManageView extends JFrame {

    private JTextField[] fields;

    public UserManageView() {
        initUI();
    }

    private void initUI() {
        setTitle("用户信息");
        setSize(400, 300);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE); // 修改为DISPOSE_ON_CLOSE
        setLocationRelativeTo(null);

        // 创建员工信息表单面板
        JPanel formPanel = createEmployeeFormPanel();
        add(formPanel, BorderLayout.CENTER);

        // 加载并显示当前用户信息
        loadUserInfo();

        setVisible(true);
    }

    private JPanel createEmployeeFormPanel() {
        fields = new JTextField[7];
        for (int i = 0; i < fields.length; i++) {
            fields[i] = new JTextField(10);
        }

        JPanel panel = new JPanel(new GridLayout(7, 2));
        panel.add(new JLabel("员工号:"));
        panel.add(fields[0]);
        panel.add(new JLabel("姓名:"));
        panel.add(fields[1]);
        panel.add(new JLabel("密码:"));
        panel.add(fields[2]);
        panel.add(new JLabel("等级:"));
        panel.add(fields[3]);
        panel.add(new JLabel("电话:"));
        panel.add(fields[4]);
        panel.add(new JLabel("薪水:"));
        panel.add(fields[5]);
        panel.add(new JLabel("备注:"));
        panel.add(fields[6]);

        return panel;
    }

    private void loadUserInfo() {
        Employee currentUser = UserSession.getInstance().getCurrentUser();

        if (currentUser != null) {
            fields[0].setText(currentUser.getEid());
            fields[0].setEditable(false); // 设置为不可编辑
            fields[1].setText(currentUser.getEname());
            fields[1].setEditable(false); // 设置为不可编辑
            fields[2].setText(currentUser.getEpw());
            fields[2].setEditable(false); // 设置为不可编辑
            fields[3].setText(currentUser.getElevel());
            fields[3].setEditable(false); // 设置为不可编辑
            fields[4].setText(currentUser.getEPhone());
            fields[4].setEditable(false); // 设置为不可编辑
            fields[5].setText(currentUser.getSalary());
            fields[5].setEditable(false); // 设置为不可编辑
            fields[6].setText(currentUser.getNotes());
            fields[6].setEditable(false); // 设置为不可编辑
        } else {
            JOptionPane.showMessageDialog(this, "未能加载用户信息", "错误", JOptionPane.ERROR_MESSAGE);
        }
    }


}
